﻿using Basic.FormModel.DB;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.Form.Model;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.Model;
using WeDonekRpc.SqlSugar;

namespace Basic.FormDAL.Repository
{
    internal class CustomFormDAL : BasicDAL<DBCustomForm, long>, ICustomFormDAL
    {
        public CustomFormDAL ( IRepository<DBCustomForm> basicDAL ) : base(basicDAL)
        {
        }
        public void LockForm ( LockFormArg arg )
        {
            if ( arg.LooseFormId.HasValue && arg.FormId.HasValue )
            {
                ISqlQueue<DBCustomForm> queue = this._BasicDAL.BeginQueue();
                queue.Update(new DBCustomForm
                {
                    Id = arg.FormId.Value,
                    IsLock = true,
                    LinkPk = arg.LinkPk,
                    LinkType = arg.LinkType
                }, "IsLock", "LinkPk", "LinkType");
                queue.Update(new DBCustomForm
                {
                    Id = arg.LooseFormId.Value,
                    IsLock = false,
                    LinkPk = arg.LinkPk,
                    LinkType = arg.LinkType
                }, "IsLock", "LinkPk", "LinkType");
                _ = queue.Submit();
            }
            else if ( arg.LooseFormId.HasValue )
            {
                if ( !this._BasicDAL.Update(new DBCustomForm
                {
                    Id = arg.FormId.Value,
                    IsLock = true,
                    LinkPk = arg.LinkPk,
                    LinkType = arg.LinkType
                }, new string[] { "IsLock", "LinkPk", "LinkType" }) )
                {
                    throw new ErrorException("form.set.fail");
                }
            }
            else if ( !this._BasicDAL.Update(new DBCustomForm
            {
                Id = arg.LooseFormId.Value,
                IsLock = false,
                LinkPk = null,
                LinkType = null
            }, new string[] { "IsLock", "LinkPk", "LinkType" }) )
            {
                throw new ErrorException("form.set.fail");
            }
        }
        public DBCustomForm Add ( FormAdd form )
        {
            DBCustomForm db = form.ConvertMap<FormAdd, DBCustomForm>();
            db.Id = IdentityHelper.CreateId();
            db.CreateTime = DateTime.Now;
            db.FormStatus = FormStatus.起草;
            base._BasicDAL.Insert(db);
            return db;
        }

        public void Drop ( long id )
        {
            if ( !this._BasicDAL.Update(a => a.IsDrop == true, a => a.Id == id) )
            {
                throw new ErrorException("form.drop.fail");
            }
        }

        public Result[] Query<Result> ( FormQuery query, IBasicPage paging, out int count ) where Result : class
        {
            paging.InitOrderBy("Id", true);
            return this._BasicDAL.Query<Result>(query.ToWhere(), paging, out count);
        }
        public void SetStatus ( DBCustomForm form, FormStatus status )
        {
            if ( !this._BasicDAL.Update(a => a.FormStatus == status, a => a.Id == form.Id) )
            {
                throw new ErrorException("form.status.set.fail");
            }
        }

        public void SetLayoutType ( long id, FormLayoutType layout )
        {
            if ( !this._BasicDAL.Update(a => a.Layout == layout, a => a.Id == id) )
            {
                throw new ErrorException("form.layout.set.fail");
            }
        }
    }
}
